<template>
	<basic-container>
		<avue-crud :option="option" :data="data" v-model="form" :page="page" ref="crud" @search-change="searchChange" @search-reset="searchReset"
		 @current-change="currentChange" @size-change="sizeChange" @row-del="rowDel" @row-update="rowUpdate" @row-save="rowSave"
		 @on-load="onLoad" :before-open="beforeOpen">
		</avue-crud>
	</basic-container>
</template>

<script>
	import request from '@/router/axios';
	import {
		getCodes
	} from "@/api/system/dict";

	export default {
		data() {
			return {
				query: {},
				page: {
					pageSize: 10,
					currentPage: 1,
					total: 0
				},
				form: {},
				option: {
					searchMenuSpan: 4,
					border: true,
					index: true,
					refreshBtn: false,
					excelBtn: true,
					menuWidth: '150px',
					column: [{
							label: "仓库编号",
							prop: "storeCode",
							width: "150px",
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "仓库名称",
							prop: "storeName",
							searchPlaceholder: '编号&名称',
							search: true,
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: '所属组织',
							prop: 'organization',
							filterable: true,
							clearable: false,
							type: 'select',
							dicData: [],
							props: {
								label: 'name',
								value: 'code'
							},
							rules: [{
								required: true,
								trigger: "blur"
							}]
						},
						{
							label: "地址",
							prop: "address"
						},
						{
							label: "面积",
							prop: "area",
							type: 'number',
							precision: 2,
							width: "100px"
						},
						{
							label: "标准容积",
							prop: "volume",
							type: 'number',
							precision: 2,
							width: "100px"
						},
						{
							label: "联系人",
							prop: "contact",
							hide: true
						},
						{
							label: "电话",
							prop: "phone",
							hide: true
						},
						{
							label: "相关人员",
							prop: "operators",
							hide: true,
							span: 24,
							multiple: true,
							filterable: true,
							type: 'select',
							dicData: [],
							props: {
								label: 'trueName',
								value: 'loginName'
							}
						}
					]
				},
				data: []
			};
		},
		created() {
			request({
				url: '2/api/BaseOrganization/getpages',
				params: {
					limit: 0
				}
			}).then(res => {
				this.findObject(this.option.column, 'organization').dicData = res.data.data;
			});
			this.request({
				url: '2/api/BaseUser/userByTenant',
				params: {
					limit: 0
				}
			}).then(res => {
				this.findObject(this.option.column, 'operators').dicData = res.data.data;
			});
		},
		methods: {
			beforeOpen(done, type) {
				if (["edit"].includes(type)) {
					this.request({
						url: '2/api/BaseUser/pages',
						params: {
							type: '2',
							where: this.form.storeCode,
							limit: 0
						}
					}).then(res => {
						const data = res.data.data
						let w = []
						data.forEach(m=>{
							w.push(m.uid)
						})
						this.form.operators = w
					});
				}
				done();
			},
			rowSave(row, loading, done) {
				request({
					url: '2/api/WmsStorage/add',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.onLoad(this.page);
					done();
				}, error => {
					done();
					console.log(error);
				});
			},
			rowUpdate(row, index, loading, done) {
				return request({
					url: '2/api/WmsStorage/edit',
					method: 'post',
					data: row
				}).then(() => {
					loading();
					this.onLoad(this.page);
				}, error => {
					done();
					console.log(error);
				});
			},
			rowDel(row) {
				this.$confirm("确定数据删除?", {
						confirmButtonText: "确定",
						cancelButtonText: "取消",
						type: "warning"
					})
					.then(() => {
						return request({
							url: '2/api/WmsStorage/remove',
							method: 'post',
							params: {
								ids: row.id
							}
						});
					})
					.then(() => {
						this.onLoad(this.page);
					});
			},
			searchReset() {
				this.query = {};
				this.page.currentPage = 1;
				this.onLoad(this.page);
			},
			searchChange(params, done) {
				this.page.currentPage = 1;
				this.query = params;
				this.onLoad(this.page);
				done();
			},
			currentChange(currentPage) {
				this.page.currentPage = currentPage;
			},
			sizeChange(pageSize) {
				this.page.pageSize = pageSize;
			},
			onLoad(page) {
				request({
					url: '2/api/WmsStorage/getpages',
					method: 'get',
					params: {
						key: this.query.storeName == undefined ? '' : this.query.storeName,
						type: this.query.storageType == undefined ? '' : this.query.storageType,
						page: page.currentPage,
						limit: page.pageSize
					}
				}).then(res => {
					const data = res.data
					this.data = data.data;
					this.page.total = data.remark;
				});
			}
		}
	};
</script>

<style>
</style>
